﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Animation;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Analyst3D;
using ESRI.ArcGIS.Geometry;

namespace SPAS
{
    #region 柳伟佳编写
    public partial class Level3DForm : Form
    {
        private IAGAnimationPlayer pAGAplayer;
        private IAGAnimationEnvironment pAGAeviroment;

        public Level3DForm()
        {
            InitializeComponent();
            this.MouseWheel += Level3DForm_MouseWheel;
        }

        public AxSceneControl SceneControl { get { return axSceneControl1; } }

        /// <summary>
        /// 根据路径创建飞行动画
        /// </summary>
        /// <param name="pScene"></param>
        /// <param name="_pPolyline"></param>
        /// <param name="_pType"></param>
        /// <param name="_pDuration"></param>
        public void CreateAnimationFromPath(IScene pScene, IPolyline _pPolyline, int _pType, double _pDuration)
        {
            IScene _pScene = pScene;

            // 获取动画扩展对象
            ESRI.ArcGIS.Analyst3D.IBasicScene2 pBasicScene2 = (ESRI.ArcGIS.Analyst3D.IBasicScene2)_pScene; // Explicit Cast
            ESRI.ArcGIS.Animation.IAnimationExtension pAnimationExtension = pBasicScene2.AnimationExtension;

            //创建两个对象，一个用于导入路径，一个用于播放
            ESRI.ArcGIS.Animation.IAGAnimationUtils pAGAnimationUtils = new ESRI.ArcGIS.Animation.AGAnimationUtilsClass();
            ESRI.ArcGIS.Animation.IAGImportPathOptions pAGImportPathOptions = new ESRI.ArcGIS.Animation.AGImportPathOptionsClass();

            // 设置参数
            pAGImportPathOptions.BasicMap = (ESRI.ArcGIS.Carto.IBasicMap)_pScene;
            pAGImportPathOptions.AnimationTracks = pAnimationExtension.AnimationTracks;
            pAGImportPathOptions.AnimationType = new AnimationTypeCameraClass();
            pAGImportPathOptions.VerticalOffset = 10;
            pAGImportPathOptions.LookaheadFactor = 1;

            pAGImportPathOptions.PutAngleCalculationMethods(esriPathAngleCalculation.esriAngleAddRelative,
                              esriPathAngleCalculation.esriAngleAddRelative,
                              esriPathAngleCalculation.esriAngleAddRelative);

            pAGImportPathOptions.AnimatedObject = _pScene.SceneGraph.ActiveViewer.Camera;
            pAGImportPathOptions.PathGeometry = _pPolyline;

            //都移动
            if (_pType == 1)
            {
                pAGImportPathOptions.ConversionType = ESRI.ArcGIS.Animation.esriFlyFromPathType.esriFlyFromPathObsAndTarget;
            }
            //观察者移动
            else if (_pType == 2)
            {
                pAGImportPathOptions.ConversionType = ESRI.ArcGIS.Animation.esriFlyFromPathType.esriFlyFromPathObserver;
            }
            //路径移动
            else
            {
                pAGImportPathOptions.ConversionType = ESRI.ArcGIS.Animation.esriFlyFromPathType.esriFlyFromPathTarget;
            }

            pAGImportPathOptions.RollFactor = 0;
            pAGImportPathOptions.AnimationEnvironment = pAnimationExtension.AnimationEnvironment;
            pAGImportPathOptions.ReversePath = false;

            ESRI.ArcGIS.Animation.IAGAnimationContainer AGAnimationContainer = pAnimationExtension.AnimationTracks.AnimationObjectContainer;
            pAGAnimationUtils.CreateFlybyFromPath(AGAnimationContainer, pAGImportPathOptions);
            //该接口相当于播放的界面
            pAGAplayer = pAGAnimationUtils as IAGAnimationPlayer;
            pAGAeviroment = new AGAnimationEnvironmentClass();
            pAGAeviroment.AnimationDuration = _pDuration;
            //pAGAeviroment.PlayMode = esriAnimationPlayMode.esriAnimationPlayOnceForward;
            pAGAeviroment.PlayMode = esriAnimationPlayMode.esriAnimationPlayLoopForward;
            pAGAplayer.PlayAnimation(_pScene as IAGAnimationTracks, pAGAeviroment, null);

        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (pAGAplayer != null && pAGAeviroment != null)
            {
                pAGAplayer.PauseAnimation();
                if (toolStripButton2.Text == "暂停" && toolStripButton1.Text == "自由查看")
                    toolStripButton2.Text = "播放";
                else if (toolStripButton1.Text == "自由查看")
                    toolStripButton2.Text = "暂停";
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (toolStripButton1.Text == "自由查看")
            {
                pAGAplayer.StopAnimation();
                toolStripButton1.Text = "动画展示";
            }
            else
            {
                pAGAplayer.PlayAnimation(axSceneControl1.Scene as IAGAnimationTracks, pAGAeviroment, null);
                toolStripButton1.Text = "自由查看";
            }

        }

        private void Level3DForm_MouseWheel(object sender, MouseEventArgs e)
        {
            try
            {
                System.Drawing.Point pSceLoc = axSceneControl1.PointToScreen(this.axSceneControl1.Location);
                System.Drawing.Point Pt = this.PointToScreen(e.Location);
                if (Pt.X < pSceLoc.X | Pt.X > pSceLoc.X + axSceneControl1.Width | Pt.Y <
                    pSceLoc.Y | Pt.Y > pSceLoc.Y + axSceneControl1.Height) return;
                double scale = 0.2;
                //if (e.Delta < 0) scale = -0.2;
                if (e.Delta > 0) scale = -0.2;
                ICamera pCamera = axSceneControl1.Camera;
                IPoint pPtObs = pCamera.Observer;
                IPoint pPtTar = pCamera.Target;
                pPtObs.X += (pPtObs.X - pPtTar.X) * scale;
                pPtObs.Y += (pPtObs.Y - pPtTar.Y) * scale;
                pPtObs.Z += (pPtObs.Z - pPtTar.Z) * scale;
                pCamera.Observer = pPtObs;
                axSceneControl1.SceneGraph.RefreshViewers();
            }
            catch
            {
            }
        }

    }
    #endregion
}
